package com.dreamwhale.machinery.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.entity.YxStoreProduct;
import com.dreamwhale.machinery.goods.entity.YxStoreOrder;
import com.dreamwhale.machinery.goods.mapper.YxStoreCartMapper;
import com.dreamwhale.machinery.service.UserApplyService;
import com.dreamwhale.machinery.user.entity.YxUser;
import com.dreamwhale.machinery.user.mapper.YxUserMapper;
import com.dreamwhale.machinery.user.service.YxUserService;
import com.dreamwhale.machinery.utils.AliSmsUtils;
import com.dreamwhale.machinery.utils.RedisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/yxUser")
public class YxUserController {
    @Resource
    private YxUserService yxUserService;

    @Resource
    private UserApplyService userApplyService;

    @Resource
    private RedisUtils<String> strRedisUtils;

    @Resource
    private AliSmsUtils aliSmsUtils;

    @Resource
    private YxUserMapper yxUserMapper;


    @Resource
    private YxStoreCartMapper yxStoreCartMapper;


    @RequestMapping("userApply")
    /**
     * @MethodName : userApply
     * @Description : 机修学堂报名
     * @param : name    姓名  
     * @param : phone   手机号
     * @Author : DragonUncle
     * @Data : 21:06 2020/6/22
     * @return :
     */
    public ResponseEntity<DataResult> userApply(String name, String phone, Integer payType, @RequestParam(defaultValue = "") Integer pid, HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getAttribute("userId").toString());

        return ResponseEntity.ok(userApplyService.userApply(name, phone, payType, userId, pid));
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    /**
     * @MethodName : userRegister
     * @Description : 用户注册
     * @param : yxUser
     * @param : code
     * @Author : DragonUncle
     * @Data : 21:08 2020/6/22
     * @return : 
     */
    public ResponseEntity<DataResult> userRegister(YxUser yxUser, String code) {
        return ResponseEntity.ok(yxUserService.userRegister(yxUser, code));
    }



    @PostMapping("/login")
    /**
     * @MethodName : userLogin
     * @Description : 用户登陆
     * @param : account
     * @param : password
     * @param : code
     * @Author : DragonUncle
     * @Data : 21:08 2020/6/22
     * @return : 
     */
    public ResponseEntity<DataResult> userLogin(String account, String password, String code) {
        return ResponseEntity.ok(yxUserService.userLogin(account, password, code));
    }

    @RequestMapping("/verificationCode")
    /**
     * @MethodName : GetVerificationCode
     * @Description : 获取验证码
     * @param : phone
     * @Author : DragonUncle
     * @Data : 21:08 2020/6/22
     * @return : 
     */
    public ResponseEntity<DataResult> GetVerificationCode(String phone) {
        return ResponseEntity.ok(yxUserService.GetVerificationCode(phone));
    }


    @RequestMapping("changeUserInfo")
    /**
     * @MethodName : changeUserInfo
     * @Description : 修改用户信息
     * @param : yxUser
     * @Author : DragonUncle
     * @Data : 19:16 2020/7/1
     * @return :
     */
    public DataResult changeUserInfo(YxUser yxUser, HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getAttribute("userId").toString());
        return yxUserService.changeUserInfo(yxUser, userId);
    }


    @RequestMapping("/personalInformation")
    /**
     * @MethodName : GetPersonalInformation
     * @Description : 获取个人信息
     * @param : request
     * @param : userId
     * @Author : DragonUncle
     * @Data : 21:09 2020/6/22
     * @return : 
     */
    public ResponseEntity<DataResult> GetPersonalInformation(Integer userId, HttpServletRequest request) {
        userId = Integer.parseInt(request.getAttribute("userId").toString());
        return ResponseEntity.ok(yxUserService.GetPersonalInformation(userId));
    }

    @RequestMapping("/inquiryOrder")
    /**
     * @MethodName : inquiryOrder
     * @Description : 查询订单
     * @param : request
     * @param : userId
     * @param : type
     * @Author : DragonUncle
     * @Data : 21:09 2020/6/22
     * @return : 
     */
    public DataResult inquiryOrder(HttpServletRequest request, Integer userId, @RequestParam(name = "type", defaultValue = "")
            Integer type, Integer page, Integer limit) {
        PageHelper.startPage(page, limit, true);
        userId = Integer.parseInt(request.getAttribute("userId").toString());
        List<YxStoreOrder> yxStoreOrder = yxUserService.inquiryOrder(userId, type);
        PageInfo<YxStoreOrder> pages = new PageInfo<>(yxStoreOrder);
        return DataResult.ok(pages);
    }

    @RequestMapping("/inquiryMyOrder")
    /**
     * @MethodName : inquiryMyOrder
     * @Description : 查询订单
     * @param : request
     * @param : userId
     * @param : type
     * @Author : DragonUncle
     * @Data : 21:09 2020/6/22
     * @return :
     */
    public ResponseEntity<DataResult> inquiryMyOrder(HttpServletRequest request, Integer userId, @RequestParam(name = "type",
            defaultValue = "") Integer type) {
        userId = Integer.parseInt(request.getAttribute("userId").toString());
        return ResponseEntity.ok(yxUserService.inquiryMyOrder(userId, type));
    }

    @RequestMapping("/orderInfo")
    /**
     * @MethodName : inquiryOrderInfo
     * @Description : 订单信息
     * @param : orderId
     * @Author : DragonUncle
     * @Data : 21:09 2020/6/22
     * @return : 
     */
    public ResponseEntity<DataResult> inquiryOrderInfo(String orderId) {
        return ResponseEntity.ok(yxUserService.inquiryOrderInfo(orderId));
    }

    @RequestMapping("/inquiryConsultingCollection")
    /**
     * @MethodName : inquiryConsultingCollection
     * @Description : 资讯查询
     * @param : userId
     * @param : type
     * @Author : DragonUncle
     * @Data : 21:09 2020/6/22
     * @return : 
     */
    public ResponseEntity<DataResult> inquiryConsultingCollection(Integer userId, Integer type, HttpServletRequest request) {
        userId = Integer.parseInt(request.getAttribute("userId").toString());
        return ResponseEntity.ok(yxUserService.inquiryConsultingCollection(userId, type));
    }

    @RequestMapping("/getUserInfo")
    /**
     * @MethodName : getUserInfo
     * @Description : 获取用户信息
     * @param : userId
     * @Author : DragonUncle
     * @Data : 21:10 2020/6/22
     * @return :
     */
    public ResponseEntity<DataResult> getUserInfo(Integer userId, HttpServletRequest request) {
        userId = Integer.parseInt(request.getAttribute("userId").toString());
        return ResponseEntity.ok(new DataResult(yxUserService.selectYxUserByUserId(userId)));
    }


    /**
     * h5注册
     * @param yxUser
     * @param code
     * @return
     */
    @RequestMapping("/userRegisterH5")
    public DataResult userRegisterH5(YxUser yxUser, String code) {
        if (StringUtils.isBlank(yxUser.getPhone())) {
            return new DataResult(300, "登录信息不完整", null);
        }
        String read = strRedisUtils.read(String.format("Sms-%s", yxUser.getPhone()));
        if (!read.equals(code)) {
            return new DataResult(300, "验证码不正确", null);
        }
        Integer phoneCount = yxUserMapper.selectCount(new QueryWrapper<YxUser>().eq("account", yxUser.getPhone()).or().eq("phone", yxUser.getPhone()));
        Integer accountCount = yxUserMapper.selectCount(new QueryWrapper<YxUser>().eq("account", yxUser.getAccount()).or().eq("phone", yxUser.getAccount()));
        if (phoneCount != 0 || accountCount != 0) {
            return new DataResult(300, "账号已被注册", null);
        }
        yxUser.setAccount(yxUser.getPhone());
        yxUser.setPassword(DigestUtils.md5DigestAsHex(yxUser.getPwd().getBytes()));
        yxUser.setSpreadUid(yxUser.getSpreadUid());
        int insert = yxUserMapper.insert(yxUser);
        if (insert == 1) {
            return new DataResult(200, "url", "http://login.jxzd.top/jxzd.apk");
        }
        return new DataResult(300, "注册失败", null);
    }
}

